package com.aim.Activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.aim.Activity.ImpoundList.MyAdapter;
import com.aim.Activity.ImpoundList.ViewHolder;
public class Notes extends Activity{
	Context mC;
	static EditText titleText, noteText;

	static CheckBox bookmark;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        mC = this;
        setContentView(R.layout.note);
        bookmark = (CheckBox) findViewById(R.id.CheckBox01);
        bookmark.setClickable(false);
        titleText = (EditText) findViewById(R.id.edittexttitle);
        noteText = (EditText) findViewById(R.id.edittextnote);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.tow_reasons, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        
        if(ImpoundTab.impoundState == 1)
		{
		
		TextView t = (TextView) findViewById(R.id.TextView12);
		t.setVisibility(View.VISIBLE);
        ListView noteList = (ListView) findViewById(R.id.listviewnote);
        SimpleAdapter	adapter1 = new SimpleAdapter(this, ImpoundTab.list,      
        		R.layout.list_item_note,      
             new String[] { "noteTitle" },       
        new int[] { R.id.textviewnotes });   

        NoteAdapter mAdapter = new NoteAdapter(mC); 
        noteList.setAdapter(mAdapter); 
        
        noteList.setOnItemClickListener(new MyOnItemClickListener2());

		}
        
        if(ImpoundTab.impoundState == 2)
		{
        	
            titleText.setText(ImpoundTab.title);
            noteText.setText(ImpoundTab.note);
            if(ImpoundTab.mark==1)
            bookmark.setChecked(true);
		
		
		}
        
        
        titleText.addTextChangedListener(new TextWatcher(){
        	public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
            	
                if (noteText.getText()+""!=""&&titleText.getText()+""!="")
                	bookmark.setClickable(true);
                else {
                	bookmark.setChecked(false);
                	bookmark.setClickable(false);
                	
                }
                
        	
        }});
        
        noteText.addTextChangedListener(new TextWatcher(){
        	
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }

            
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }

            
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                if (noteText.getText()+""!=""&&titleText.getText()+""!="")
                	bookmark.setClickable(true);
                else {
                	bookmark.setChecked(false);
                	bookmark.setClickable(false);
                	
                }
        	
        }});
        
	}
	
	public void finish(){
		
		mC = null;
		titleText = null;
		noteText= null;
		bookmark= null;
		
		super.finish();
	}

    public static void setNotes(){
    	
    	ImpoundTab.bookmark = bookmark.isChecked();

    	if(bookmark.isChecked()) ImpoundTab.mark=1;
    	else ImpoundTab.mark = 0;
    	ImpoundTab.note= noteText.getText().toString();
    	ImpoundTab.title = titleText.getText().toString();
    	
    	
    }
}
class NoteAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    public NoteAdapter(Context context) { 
        this.mInflater = LayoutInflater.from(context); 
    }

    
    public int getCount() { 
        // TODO Auto-generated method stub 
        return ImpoundTab.list.size(); 
    }

     
    public Object getItem(int arg0) { 
        // TODO Auto-generated method stub 
        return null; 
    }

     
    public long getItemId(int arg0) { 
        // TODO Auto-generated method stub 
        return 0; 
    }

    
    public View getView(int position, View convertView, ViewGroup parent) {
    	TextView note_item;
//        ViewHolder holder = null; 
        if (convertView == null) {

//            holder = new ViewHolder();

            convertView = mInflater.inflate(R.layout.list_item_note, null); 
            note_item = (TextView) convertView.findViewById(R.id.textviewnotes); 
            

            convertView.setTag(note_item);

        } else {
        	note_item = (TextView) convertView.getTag(); 
        } 
        String a = (String)ImpoundTab.list.get(position).get("bookmark");
        if(a.equals("1"))
        {
        	note_item.setTextColor(Color.parseColor("#f36c29"));
        }
        else 
//        	note_item.setTextColor(Color.GREEN);
        
        
        note_item.setText((String)ImpoundTab.list.get(position).get("noteTitle")); 
       
        

        return convertView; 
    }
}

class  MyOnItemClickListener2  implements OnItemClickListener {
	public void onItemClick(AdapterView<?> parent,
	        View view, int pos, long id) {
		LayoutInflater factory = LayoutInflater.from(view.getContext());
   	 final View textEntryView = factory.inflate(R.layout.notedialog, null);
   	
   		CheckBox cb = (CheckBox) textEntryView.findViewById(R.id.checkBox1);
   		if(((String)ImpoundTab.list.get(pos).get("bookmark")).equals("1"))
   		cb.setChecked(true);
	   EditText title=(EditText) textEntryView.findViewById(R.id.edittexttitle1);
	   title.setText((String)ImpoundTab.list.get(pos).get("noteTitle"));
	   EditText note=(EditText) textEntryView.findViewById(R.id.edittextnote1);
	   note.setText((String)ImpoundTab.list.get(pos).get("notes"));
	   
   	  new AlertDialog.Builder(view.getContext())

   	 .setTitle("Note Detail")
   	 .setView(textEntryView)
   	 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
   	  public void onClick(DialogInterface dialog, int whichButton) {

   		dialog.dismiss();
   	   
   	  }
   	 })
//   	 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
//   	  public void onClick(DialogInterface dialog, int whichButton) {
//   	  /* User clicked cancel so do some stuff */
//   		  dialog.dismiss();
//   	  }
//   	 })
   	 .create().show();
	    	
	    }

	    public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    }
	
}


